package org.fuys.coder.domain.act.lottery.service.activity.impl;

import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.common.util.DateUtil;
import org.fuys.coder.domain.act.lottery.model.req.draw.DrawReq;
import org.fuys.coder.domain.act.lottery.model.res.draw.DrawResult;
import org.fuys.coder.domain.act.lottery.model.vo.activity.LotteryActSupportVO;
import org.fuys.coder.domain.act.lottery.model.vo.activity.LotteryActVO;
import org.fuys.coder.domain.act.lottery.model.vo.award.AwardVO;
import org.fuys.coder.domain.act.lottery.repository.ILotteryActAwardRepository;
import org.fuys.coder.domain.act.lottery.repository.ILotteryActivityRepository;
import org.fuys.coder.domain.act.lottery.model.req.act.LotteryActivityAddReq;
import org.fuys.coder.domain.act.lottery.service.activity.ILotteryActivityService;
import org.fuys.coder.domain.act.lottery.service.strategy.ILotteryStrategyService;
import org.fuys.coder.domain.act.support.model.vo.ActRedemptionVO;
import org.fuys.coder.domain.act.support.model.vo.ActTypeVO;
import org.fuys.coder.domain.act.support.model.vo.ActVO;
import org.fuys.coder.common.entity.PageDTO;
import org.fuys.coder.domain.file.service.AliYunOSSService;
import org.fuys.coder.domain.mailbox.service.IMailBoxService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.lottery.service.activity.impl
 * @className: LotteryActivityServiceImpl
 * @author: WangJing
 * @description: 抽奖活动实现类
 * @date: 2024/7/8 23:03
 * @version: 1.0
 */
@Service
public class LotteryActivityServiceImpl implements ILotteryActivityService {

    @Resource
    private ILotteryActivityRepository lotteryActivityRepository;

    @Resource
    private ILotteryActAwardRepository lotteryActAwardRepository;

    @Resource
    private ILotteryStrategyService lotteryStrategyService;

    @Resource
    private IMailBoxService mailBoxService;

    @Resource
    private AliYunOSSService aliYunOSSService;

    @Transactional
    @Override
    public void addActivity(LotteryActivityAddReq req) {
        try {
            final Long aLong = lotteryActivityRepository.addLotteryActInfo(req);
            lotteryActAwardRepository.addLotteryAwardIds(req.getAwards(),aLong);
//            mailBoxService.publishMail();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public LotteryActVO getLotteryById(Long actId,Integer state) {
        final LotteryActVO byId = lotteryActivityRepository.getById(actId, state);
        if(byId==null){
            return null;
        }
//        byId.setCoverUrl(aliYunOSSService.buildUrl(byId.getCoverId()));
        final LotteryActSupportVO lotterySupportVO = this.getLotterySupportVO(actId);
        byId.setSupportVO(lotterySupportVO);
        return byId;
    }

    @Override
    public LotteryActSupportVO getLotterySupportVO(Long actId) {
        List<AwardVO> awardVOList = lotteryActAwardRepository.getLotteryAwardsByActId(actId);
        awardVOList.forEach(item->{
            item.setCoverUrl(aliYunOSSService.buildUrl(item.getAwardId()));
        });
        if(ObjectUtils.isEmpty(awardVOList)){
            return null;
        }
        LotteryActSupportVO supportVO=new LotteryActSupportVO();
        supportVO.setAwardList(awardVOList);
        return supportVO;
    }

    @Override
    public List<ActVO> getCurHotAct(int num) {
        List<LotteryActVO> actList = lotteryActivityRepository.getCurAct(num);
        if(ObjectUtils.isEmpty(actList)){
            return null;
        }
        return actList.stream().map(item -> {
            item.setCoverUrl(aliYunOSSService.buildUrl(item.getCoverId()));
            ActVO actVO = new ActVO();
            actVO.setActData(item);
            return actVO;
        }).collect(Collectors.toList());
    }

    @Override
    public Pair<List<ActVO>,Long> getPage(PageDTO pageDTO) {
        Pair<List<LotteryActVO>,Long> actList = lotteryActivityRepository.getPage(pageDTO);
        if(ObjectUtils.isEmpty(actList)){
            return null;
        }
        final List<ActVO> collect = actList.getLeft().stream().map(item -> {
            item.setCoverUrl(aliYunOSSService.buildUrl(item.getCoverId()));
            ActVO actVO = new ActVO();
            actVO.setActData(item);
            actVO.setType(ActTypeVO.LOTTERY);
            return actVO;
        }).collect(Collectors.toList());
        return new Pair<>(collect,actList.getRight());
    }

    @Override
    public boolean checkTakeCount(Integer userId,Long actId) {
        return lotteryActivityRepository.checkUserTakeCount(userId,actId);
    }

    @Override
    public DrawReq getLotteryStrategyById(Long actId) {
        Integer strategyId=lotteryActivityRepository.getStrategyId(actId);
        return lotteryStrategyService.getActStrategy(strategyId);
    }

    @Override
    public ActRedemptionVO createAwardRedemption(Integer userId,Long actId, DrawResult drawResult) {
        //生成兑换码 和 过期时间
        String code= UUID.randomUUID().toString();
        final Date date = DateUtil.addTimeFromCur(7, TimeUnit.DAYS);
        //数据库查询奖品操作 可以添加更多过滤器
        final List<AwardVO> awards = lotteryActAwardRepository.getLotteryAwardsByActId(actId);
        final AwardVO awardVO = awards.get(drawResult.getOrder()-1);
        //数据库插入数据操作
        if(awardVO==null){
            return new ActRedemptionVO();
        }
        lotteryActivityRepository.addLotteryTakeRecord(userId,actId,code,awardVO.getAwardId(),date,awardVO.getAwardType());
        ActRedemptionVO vo=new ActRedemptionVO();
        vo.setCode(code);
        vo.setExpire(date.getTime());
        vo.setIndex(drawResult.getOrder());
        vo.setMsg(drawResult.getMsg());
        return vo;
    }
}
